package Array;

import org.junit.Test;

import java.util.Arrays;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Array
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-21  23:18
 * @Description: TODO
 * @Version: 1.0
 */
public class 分发糖果135 {

    // 采用贪心的思路 就是模拟遍，然后在通过比较获取当前位置最大值
    public int candy(int[] ratings) {
        // 初始判断条件
        if (ratings.length <= 1) {
            return ratings.length;
        }
        int[] result = new int[ratings.length];
        Arrays.fill(result, 1);
        // 开始遍历左边到右边 如果右边的大 那就result[i] = result[i - 1] + 1;
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i - 1]) {
                result[i] = result[i - 1] + 1;
            }
        }
        int sum = 0;
        // 开始遍历右边边到左边 如果左边的大那就result[i] = Math.max(result[i], result[i + 1] + 1);
        for (int i = ratings.length - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                result[i] = Math.max(result[i], result[i + 1] + 1);
            }
        }
        for (int key : result) {
            sum += key;
        }
        System.out.println(Arrays.toString(result));
        return sum;
    }

    @Test
    public void test() {
        int candy = candy(new int[]{1, 0, 2});
        System.out.println(candy);
    }
}
